<?php
class setting extends core {
	public function __construct() {
		parent::__construct();
	}

	//index
	public function index() {
		$this->manager();
	}

	//修改密码
	public function password() {
		if (IS_POST) {
			$new_password = isset($_POST['new_password']) ? $_POST['new_password'] : '';
			$confirm_password = isset($_POST['confirm_password']) ? $_POST['confirm_password'] : '';
			if ($new_password && $new_password == $confirm_password) {
				$salt = generate_salt();
				$new_password = crypt_password($new_password, $salt);
				$sql = "UPDATE {$this->tbp}admin SET password='{$new_password}', salt='{$salt}' WHERE id='{$this->admin_id}'";
				$this->db->query($sql);
				header("Location: ?app=setting&act=password&msg=1");
			} else {
				error("两次输入的密码不一致");
			}
		} else {
			$msg = isset($_GET['msg']) ? 1 : 0;
			$this->smarty->assign("msg", $msg);
			$this->display();
		}
	}

	//个人资料
	public function info() {
		if (IS_POST) {
			$real_name = isset($_POST['real_name']) ? $_POST['real_name'] : '';
			$qq = isset($_POST['qq']) ? $_POST['qq'] : '';
			$mobile = isset($_POST['mobile']) ? $_POST['mobile'] : '';
			$weixin = isset($_POST['weixin']) ? $_POST['weixin'] : '';
			$sql = "UPDATE {$this->tbp}admin SET real_name='{$real_name}', qq='{$qq}', mobile='{$mobile}', weixin='{$weixin}' WHERE id='{$this->admin_id}'";
			$this->db->query($sql);
			header("Location: ?app=setting&act=info&msg=1");
		} else {
			$msg = isset($_GET['msg']) ? 1 : 0;
			$sql = "SELECT * FROM {$this->tbp}admin WHERE id='{$this->admin_id}'";
			$row = $this->db->get_row($sql);
			$this->smarty->assign("row", $row);
			$this->smarty->assign("msg", $msg);
			$this->display();
		}
	}
	
	//管理员列表
	public function manager() {
		$where = '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$id = (isset($_GET['id'])) ? trim($_GET['id']) : '';

		if ($keyword) {
			$where .= " AND (name LIKE '%{$keyword}%' OR real_name LIKE '%{$keyword}%')";
		}
		if (strlen($id)) {
			$where .= " AND id='{$id}' ";
		}
		//分页
		$this->setpages(array('id'=>$id, 'keyword'=>$keyword));

		$sql = "SELECT *
			FROM {$this->tbp}admin
			WHERE 1 {$where} ORDER BY id ASC";
		// echo $sql;
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('id',$id);
		$this->smarty->assign('page',$nav);

		$this->smarty->assign('is_admin', $_SESSION['admin']->is_admin);
		$this->display('setting.manager.html');
	}

	//添加管理员
	public function manager_add() {
		if (IS_POST) {
			$id = isset($_POST['password']) ? intval($_POST['id']) : 0;
			$is_admin = isset($_POST['is_admin']) ? intval($_POST['is_admin']) : 0;
			$name = isset($_POST['name']) ? trim($_POST['name']) : '';
			$real_name = isset($_POST['real_name']) ? trim($_POST['real_name']) : '';
			$password = isset($_POST['password']) ? $_POST['password'] : '';
			$permissions = isset($_POST['permissions']) ? $_POST['permissions'] : '';
			if ($id>0) {
				$set = '';
				if ($password) {
					$salt = generate_salt();
					$password = crypt_password($password, $salt);
					$set = ", password='{$password}', salt='{$salt}' ";
				}
				$sql = "UPDATE {$this->tbp}admin SET name='{$name}', real_name='{$real_name}', is_admin='{$is_admin}' {$set} WHERE id='{$id}'";
				$this->db->query($sql);
				$url = "?app=setting&act=manager_add&id={$id}&msg=1";
			} else {
				$salt = generate_salt();
				$password = crypt_password($password, $salt);
				$sql = "INSERT INTO {$this->tbp}admin(name, real_name, password, salt, is_admin, add_time)
					VALUES('{$name}', '{$real_name}', '{$password}', '{$salt}', '{$is_admin}', '".time()."')";
				$this->db->query($sql);
				$id = $this->db->insert_id;
				$url = "?app=setting&act=manager";
			}
			//处理权限
			header("Location: {$url}");
		} else {
			$current_permissions = "";
			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
			if ($id) {
				$sql = "SELECT * FROM {$this->tbp}admin WHERE id='{$id}'";
				$row = $this->db->get_row($sql);
				$row = array("id"=>$row->id, "name"=>$row->name, "real_name"=>$row->real_name, "is_admin"=>$row->is_admin);
			} else {
				$row = array("id"=>0, "name"=>"", "real_name"=>"", "is_admin"=>0);
			}
			$msg = isset($_GET['msg']) ? 1 : 0;
			$this->smarty->assign("msg", $msg);
			$this->smarty->assign('row',$row);
			$this->display();
		}
	}

	//删除管理员
	public function manager_delete() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "DELETE FROM {$this->tbp}admin WHERE id='{$id}'";
		$this->db->query($sql);
		header("Location:?app=setting&act=manager");
	}


	//开放的城市
	public function city() {
		$where = '';
		//分页
		$this->ezr->num_results_per_page = 20;
		$sql = "SELECT * FROM {$this->tbp}open_city WHERE 1 {$where} ORDER BY id DESC";
		// echo $sql;
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
			}
		}
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);
		$this->display();
	}

	//开放的区域
	public function district() {
		$where = '';
		$id = (isset($_GET['id']) && intval($_GET['id'])) ? intval($_GET['id']) : 0;
		$city_id = (isset($_GET['city_id']) && intval($_GET['city_id'])) ? intval($_GET['city_id']) : 0;
		if ($city_id>0) $where .= " AND city_id='{$city_id}'";
		//分页
		$this->ezr->num_results_per_page = 20;
		$this->setpages(array('city_id'=>$city_id));
		$sql = "SELECT * FROM {$this->tbp}open_district WHERE 1 {$where} ORDER BY id DESC";
		// echo $sql;
		$rs = $this->ezr->get_results($sql);
		// if ($rs) {
		// 	foreach ($rs as $key => $row) {
		// 	}
		// }
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);

		$this->smarty->assign('id',$id);

		if ($id>0) {
			$sql = "SELECT * FROM {$this->tbp}open_district WHERE id='{$id}'";
			$city = $this->db->get_row($sql);
			$this->smarty->assign('row', $city);
		} else {
			$row = new stdClass();
			$row->name = '';
			$row->sort = 999;
			$row->district_id = '';
			$this->smarty->assign('row',$row);
		}
		$this->display();
	}

	//添加区域
	public function district_add() {
		if (IS_POST) {
			$this->district_save();
		} else {
			exit;
		}
	}

	//编辑区域
	public function district_edit() {
		if (IS_POST) {
			$this->district_save();
		} else {
			$id = (isset($_GET['id']) && intval($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM {$this->tbp}open_district WHERE id='{$id}'";
			$city = $this->db->get_row($sql);
			$this->smarty->assign('row', $city);
			$this->smarty->assign('id',$id);
			$this->display();
		}
	}

	//保存区域
	public function district_save() {
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$city_id = (isset($_POST['city_id']) && intval($_POST['city_id'])) ? intval($_POST['city_id']) : 0;
		$district_id = (isset($_POST['district_id']) && intval($_POST['district_id'])) ? intval($_POST['district_id']) : 0;
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
		$sort = isset($_POST['sort']) ? trim($_POST['sort']) : '';

		if ($id>0) {
			$sql = "UPDATE {$this->tbp}open_district SET name='{$name}', sort='{$sort}', city_id='{$city_id}', district_id='{$district_id}' WHERE id='{$id}'";
			$this->db->query($sql);
		} else {
			$sql = "INSERT INTO {$this->tbp}open_district(name, sort, city_id, district_id) VALUES('{$name}', '{$sort}', '{$city_id}', '{$district_id}')";
			$this->db->query($sql);
			$id = $this->db->insert_id;
		}
		//echo $sql;
		//echo $city_id;
		//exit;
		header("Location:?app=setting&act=district");
	}

	//删除区域
	public function district_delete() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$city_id = (isset($_GET['city_id']) && trim($_GET['city_id'])) ? intval($_GET['city_id']) : 0;
		$sql = "DELETE FROM {$this->tbp}open_district WHERE id='{$id}'";
		$this->db->query($sql);
		header("Location:?app=setting&act=district&city_id={$city_id}");
	}


	//参数配置
	public function configs() {
		$where = '';
		$name = (isset($_GET['name']) && trim($_GET['name'])) ? trim($_GET['name']) : '';
		$memo = (isset($_GET['memo']) && trim($_GET['memo'])) ? trim($_GET['memo']) : '';
		if ($name) {
			$where .= " AND name LIKE '%{$name}%' ";
		}
		if ($memo) {
			$where .= " AND memo LIKE '%{$memo}%' ";
		}
		//分页
		$this->setpages(array('name'=>$name, 'memo'=>$memo));
		$this->ezr->num_results_per_page = 20;
		$sql = "SELECT * FROM {$this->tbp}config WHERE status='1' {$where} ORDER BY id ASC";
		// echo $sql;
		$rs = $this->ezr->get_results($sql);
		// if ($rs) {
		// 	foreach ($rs as $key => $row) {
		// 	}
		// }
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('name',$name);
		$this->smarty->assign('memo',$memo);
		$this->smarty->assign('page',$nav);
		$this->display();
	}

	//参数修改
	public function configs_edit() {
		if (IS_POST) {
			$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
			$origin_content = (isset($_POST['origin_content']) && trim($_POST['origin_content'])) ? trim($_POST['origin_content']) : '';
			$content = isset($_POST['content']) ? $_POST['content'] : '';
			$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
			$memo = (isset($_POST['memo']) && trim($_POST['memo'])) ? trim($_POST['memo']) : '';
			if ($id) {
				$sql = "SELECT count(*) FROM {$this->tbp}config WHERE id!='{$id}' AND name='{$name}'";
				if (intval($this->db->get_var($sql))) error('该参数名称已存在');
				if ($config->type=='file') {
					$content = isset($_FILES['content']) ? $_FILES['content'] : '';
					if (!$content) {
						$content = $origin_content;
					} else {
						$content = upload_one_file('pic', 'content');
					}
				} else {
					$content = trim($content);
				}
				$sql = "UPDATE {$this->tbp}config SET name='{$name}', memo='{$memo}', content='{$content}' WHERE id='{$id}'";
				$this->db->query($sql);
			} else {
				
			}
			header("Location: ?app=setting&act=configs_edit&id={$id}&msg=1");
		} else {
			$msg = isset($_GET['msg']) ? 1 : 0;
			$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM {$this->tbp}config WHERE id='{$id}'";
			$row = $this->db->get_row($sql);
			$this->smarty->assign("row", $row);
			$this->smarty->assign("msg", $msg);
			$this->display();
		}
	}

	//日志列表
	public function log() {
		$log_mod = m("log");
		$where = '';
		$member_name = (isset($_GET['member_name']) && trim($_GET['member_name'])) ? trim($_GET['member_name']) : '';
		$log_app = (isset($_GET['log_app']) && trim($_GET['log_app'])) ? trim($_GET['log_app']) : '';
		$log_act = (isset($_GET['log_act']) && trim($_GET['log_act'])) ? trim($_GET['log_act']) : '';
		$ip = (isset($_GET['ip']) && trim($_GET['ip'])) ? trim($_GET['ip']) : '';
		$id = (isset($_GET['id'])) ? trim($_GET['id']) : '';

		if ($member_name) {
			$where .= "AND member_name='{$member_name}' ";
		}
		if ($log_app) {
			$where .= "AND app='{$log_app}' ";
		}
		if ($log_act) {
			$where .= "AND act='{$log_act}' ";
		}
		if ($ip) {
			$where .= "AND ip='{$ip}' ";
		}
		if (strlen($id)) {
			$where .= "AND id='{$id}' ";
		}
		//分页
		$this->setpages(array('id'=>$id, 'member_name'=>$member_name, 'log_app'=>$log_app, 'log_act'=>$log_act, 'ip'=>$ip));
		$this->ezr->num_results_per_page = 20;
		$sql = "SELECT *
			FROM {$this->tbp}log
			WHERE 1 {$where} ORDER BY id DESC";
		// echo $sql;
		$rs = $this->ezr->get_results($sql);
		/*
		if ($rs) {
			foreach ($rs as $key => $row) {
				$app_name = $log_mod->convert_app_name($row['app']);
				$act_name = $log_mod->convert_act_name($row['act']);
				if (!$app_name) $app_name = $row['app'];
				if (!$act_name) $act_name = $row['act'];
				$rs[$key]['app'] = "{$app_name}";
				$rs[$key]['act'] = "{$act_name}";
			}
		}
		*/
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('member_name',$member_name);
		$this->smarty->assign('log_app',$log_app);
		$this->smarty->assign('log_act',$log_act);
		$this->smarty->assign('ip',$ip);
		$this->smarty->assign('id',$id);
		$this->smarty->assign('page',$nav);

		$this->display('setting.log.html');
	}

	//日志列表
	public function gm_log() {
		$log_mod = m("log");
		$where = '';
		$member_name = (isset($_GET['member_name']) && trim($_GET['member_name'])) ? trim($_GET['member_name']) : '';
		$log_app = (isset($_GET['log_app']) && trim($_GET['log_app'])) ? trim($_GET['log_app']) : '';
		$log_act = (isset($_GET['log_act']) && trim($_GET['log_act'])) ? trim($_GET['log_act']) : '';
		$ip = (isset($_GET['ip']) && trim($_GET['ip'])) ? trim($_GET['ip']) : '';
		$id = (isset($_GET['id'])) ? trim($_GET['id']) : '';

		if ($member_name) {
			$where .= "AND member_name='{$member_name}' ";
		}
		if ($log_app) {
			$where .= "AND app='{$log_app}' ";
		}
		if ($log_act) {
			$where .= "AND act='{$log_act}' ";
		}
		if ($ip) {
			$where .= "AND ip='{$ip}' ";
		}
		if (strlen($id)) {
			$where .= "AND id='{$id}' ";
		}
		//分页
		$this->setpages(array('id'=>$id, 'member_name'=>$member_name, 'log_app'=>$log_app, 'log_act'=>$log_act, 'ip'=>$ip));
		$this->ezr->num_results_per_page = 20;
		$sql = "SELECT *
			FROM {$this->tbp}log
			WHERE script_name LIKE '/gm/%' {$where} ORDER BY id DESC";
		// echo $sql;
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$memo = $log_mod->get_log_name($row['app'], $row['act']);
				if (!$memo) {
					$idx = $row['app']."_".$row['act'];
					if (array_key_exists($idx, $log_mod->app_acts)) {
						$memo = $log_mod->app_acts[$idx];
					}
				}
				$rs[$key]['memo'] = $memo;
			}
		}
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('member_name',$member_name);
		$this->smarty->assign('log_app',$log_app);
		$this->smarty->assign('log_act',$log_act);
		$this->smarty->assign('ip',$ip);
		$this->smarty->assign('id',$id);
		$this->smarty->assign('page',$nav);

		$this->display('setting.gm_log.html');
	}

}
